package yns.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import yns.base.BaseSupport;
import yns.entity.User;
import yns.http.HttpHelper;
import yns.http.HttpRequest;
import yns.http.HttpResponse;

//@RestController Controller如果都是rest接口，可以直接使用这个注解，方法不用再添加@ResponseBody
@Controller
public class TestController {


	//spring mvc 基本使用
	@RequestMapping(value = "/test")
	public ModelAndView test() {
		System.out.println("test");
		ModelAndView mv = new ModelAndView();

		mv.addObject("message", "<a>Hello World!</a>");

		mv.setViewName("test");

		return mv;
	}

	//spring mvc 返回json数据，指定name变量非必须
	@RequestMapping(value = "/test2", produces = { BaseSupport.JSON_UTF8 })
	@ResponseBody
	public String test2(@RequestParam(value = "name", required = false) String name) throws IOException {
		System.out.println("test2");
   

		HttpRequest httpRequest = new HttpRequest();
		HttpResponse httpResponse = new HttpResponse();
		HttpHelper httpHelper = new HttpHelper();
		httpRequest.setSendURL("http://www.baidu.com");
		
		httpHelper.execute(httpRequest, httpResponse);
		String html = httpResponse.getResponseResult();

		System.out.println(name);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("info", "中文");
		jsonObject.put("key2", new JSONArray());
		jsonObject.put("html", html);

		return jsonObject.toString();

	}
	
	//spring mvc 返回json数据，映射实体User,没有的属性会使用构造函数的值初始化
	@RequestMapping(value = "/test3", produces = { BaseSupport.JSON_UTF8 })
	@ResponseBody
	public String test4(@ModelAttribute("user") User user) {
		System.out.println("test3");

		System.out.println(user.getName());
		System.out.println(user.getAge());

		List<User> list = new ArrayList<User>();
		for (int i = 0; i < 3; i++) {
			User listUser = new User();
			listUser.setName("这是中文" + i);
			listUser.setAge(String.valueOf(i));
			list.add(listUser);
		}
		
		System.out.println(JSONObject.valueToString(user));
		System.out.println(JSONObject.valueToString(list));
		
		return JSONObject.valueToString(list);

	}

	//spring mvc rest风格URL，获取rest URL中的参数
	@RequestMapping(value = "/test4/{msg}", produces = { BaseSupport.JSON_UTF8 })
	@ResponseBody
	public String test4(@PathVariable(value = "msg") String msg, User user) {
		System.out.println("test4");
		System.out.println(msg);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("key", user.getName());
		jsonObject.put("key2", msg);

		return jsonObject.toString();

	}

	//spring mvc 接受含指定变量的访问
	@RequestMapping(value = "/test5", params = "submitFlag=create", produces = { BaseSupport.JSON_UTF8 })
	@ResponseBody
	public String test5(String msg, User user, String submitFlag) {
		System.out.println("test5");

		System.out.println(submitFlag);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("key", user.getName());
		jsonObject.put("key2", msg);

		return jsonObject.toString();

	}
	
	//spring mvc 多个URL映射同一个方法(特别的，多与一级静态资源的相对路径需要对应多一级)
	//  /test6 对应  js/jslib/jquery/jquery-1.11.1.min.js
	//  /test6/test 对应    ../js/jslib/jquery/jquery-1.11.1.min.js
	@RequestMapping(value = {"/test6", "/test6/test"})
	public ModelAndView test6() {
		System.out.println("test2");
		ModelAndView mv = new ModelAndView();

		mv.addObject("message", "Hello World!");

		mv.setViewName("test");
		return mv;
	}
	
	//spring mvc 只接受post请求
	@RequestMapping(value = "/test7",produces = {BaseSupport.JSON_UTF8},method = RequestMethod.POST)
	@ResponseBody
	public String test7(@RequestParam(value="name",required=false)  String name) {
		System.out.println("test5");
		
		System.out.println(name);
		
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("key", "value");
		jsonObject.put("key2", new JSONArray());
		
		return jsonObject.toString();
		
	}
}
